package com.lmf.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lmf.eduservice.entity.EduSubject;
import com.lmf.eduservice.entity.excel.SubjectData;
import com.lmf.eduservice.service.EduSubjectService;
import com.lmf.handler.Exception.LmfException;
import lombok.SneakyThrows;

/**
 * 创建excel监听器，用于读取subjectData实体类对应表中的数据
 */
public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {

    //因为SubjectExcelListener没有交给spring进行ioc管理，需要自己手动new，不能注入其他对象，因此不能实现数据库操作

    private EduSubjectService eduSubjectService;

    public SubjectExcelListener() {
    }

    //可以通过有参构造方法，将操作数据库的变量方法传递进来
    public SubjectExcelListener(EduSubjectService eduSubjectService) {
        this.eduSubjectService = eduSubjectService;
    }

    @SneakyThrows
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        //判断上传的文件中是否存在数据
        //表示excel中没有数据，就不需要读取了
        if (subjectData == null) {
            throw new LmfException(20001,"上传文件数据为空");
        }

        //从数据库中，获取某个一级分类科目对象
        EduSubject oneEduSubject = this.existOneSubject(eduSubjectService, subjectData.getOneSubjectName());
        //如果该科目查询为空，则开始添加一级科目
        if (oneEduSubject == null) {
            //给这个对象重新赋予新的对象数据
            oneEduSubject = new EduSubject();
            oneEduSubject.setParentId("0");//0表示一级科目
            oneEduSubject.setTitle(subjectData.getOneSubjectName());//设置一级课程名
            eduSubjectService.save(oneEduSubject);//往数据库里面添加一条记录(一级科目)
        }

        //获取一级课程的parentId值
        String parentId = oneEduSubject.getId();
        //从数据库中，获取某个二级分类科目对象
        EduSubject twoEduSubject = this.existTwoSubject(eduSubjectService, subjectData.getTwoSubjectName(), parentId);
        //如果该科目为空，则开始添加二级科目
        if (twoEduSubject == null) {
            twoEduSubject = new EduSubject();
            twoEduSubject.setParentId(parentId);
            twoEduSubject.setTitle(subjectData.getTwoSubjectName());
            eduSubjectService.save(twoEduSubject);
        }

    }

    //查询一级分类课目是否存在，不能重复添加
    public EduSubject existOneSubject(EduSubjectService eduSubjectService,String name){
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name)
                .eq("parent_id","0");
        EduSubject oneSubject = eduSubjectService.getOne(wrapper);
        return oneSubject;
    }

    //查询二级分类课目是否存在，不能重复添加
    public EduSubject existTwoSubject(EduSubjectService eduSubjectService,String name,String parentId){
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name)
                .eq("parent_id",parentId);
        EduSubject twoSubject = eduSubjectService.getOne(wrapper);
        return twoSubject;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
